//
//  XMScondCtr.m
//  TestView
//
//  Created by lk@wxm on 2024/12/4.
//

#import "XMScondCtr.h"

@interface XMScondCtr ()<UICollectionViewDelegate,UICollectionViewDataSource>
//@property (nonatomic, assign) NSInteger boardSize;   // 棋盘大小：8 或 10
@end

@implementation XMScondCtr
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    // Do any additional setup after loading the view.
    self.title = @"棋局";
    self.boardSize = 8;
    
    // 创建一个流式布局（Flow Layout）
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的尺寸
    layout.itemSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width/self.boardSize, [[UIScreen mainScreen] bounds].size.width/self.boardSize);

    // 设置每行之间的间距
    layout.minimumLineSpacing = 0;

    // 设置每个item之间的间距
    layout.minimumInteritemSpacing = 0;

    // 设置边距
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 创建一个UICollectionView，指定其布局
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor grayColor];

    // 注册Cell类型
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    // 添加到视图中
    [self.view addSubview:collectionView];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.boardSize * self.boardSize;
}


// 返回单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    cell.contentView.layer.borderWidth = 0.5;
    cell.contentView.layer.borderColor = [UIColor grayColor].CGColor;
    NSInteger x = (self.boardSize - 2) / 2;
    
    if (indexPath.row == x + self.boardSize * x){
        [self addTem:cell.contentView andColor:[UIColor whiteColor]];
    }
    if (indexPath.row == x + self.boardSize * x + 1){
        [self addTem:cell.contentView andColor:[UIColor blackColor]];
    }
    if (indexPath.row == x + self.boardSize * (x+1) ){
        [self addTem:cell.contentView andColor:[UIColor blackColor]];
    }
    if (indexPath.row == x + self.boardSize * (x+1) + 1){
      
        [self addTem:cell.contentView andColor:[UIColor whiteColor]];
    }
    return cell;
}
- (void)addTem:(UIView*)cell andColor:(UIColor *)color{
    UILabel *tem = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width/self.boardSize, [[UIScreen mainScreen] bounds].size.width/self.boardSize)];
    // 设置圆角
    tem.layer.cornerRadius = ([[UIScreen mainScreen] bounds].size.width / self.boardSize) / 2;  // 设置为宽度的一半，使其为圆形
    tem.layer.masksToBounds = YES;  // 必须设置为YES，才能让圆角生效

    // 如果需要其他属性，可以在这里进行设置，例如：
    tem.backgroundColor = color; // 设置背景颜色
    tem.textAlignment = NSTextAlignmentCenter;  // 设置文本居中
    tem.textColor = [UIColor whiteColor];  // 设置文本颜色
    tem.font = [UIFont systemFontOfSize:14];  // 设置字体大小

    // 添加到父视图中
    [cell addSubview:tem];
}
@end




